package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Exam;
import com.han.bom.Professor;
import com.han.bom.Student;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class ActivatedExamsStudentsListServlet
 */
@WebServlet("/Professor/ActivatedExamsStudentsListServlet")
public class ActivatedExamsStudentsListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(ActivatedExamsStudentsListServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActivatedExamsStudentsListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered ActivatedExamsStudentsListServlet GET");
		
		Professor professor = null;
		Exam exam = null;
		HttpSession session = request.getSession();
		ExamDAO examdao = new ExamDAO();
		UserDAO userdao = new UserDAO();

		professor = (Professor) session.getAttribute("professor");
		
		if(professor!=null){
			
		}
		
		if(request.getParameter("examID")!=null){
			try {
				exam = examdao.retrieveExamById(request.getParameter("examID"));
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		}
		else{
			
		}

		//System.out.println(request.getParameter("examID"));
		
		try {
			
			ArrayList<Student> currentlyTaking = userdao.retrieveStudentsTakingExam(exam.getExamID());
			ArrayList<Student> doneTaking = userdao.retrieveStudentsByExamTaken(exam.getExamID());
			ArrayList<Student> notYetTaking = userdao.retrieveStudentsIncludedInExam(exam.getExamID());
			
			RequestDispatcher rd = request.getRequestDispatcher("Exam_Real_Status.jsp");
			
			request.setAttribute("currentlyTaking", currentlyTaking);
			request.setAttribute("doneTaking", doneTaking);
			request.setAttribute("notYetTaking", notYetTaking);
			
			rd.forward(request, response);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
